/**
 * related to userMessage.ui
 * 
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-19
 */
var nf = sm("do_Notification");
var app = sm("do_App");
var Controller = require("core/controller");
var log = require("util/log")
var navigate = require("util/navigate");
var net = require("util/net");
var server = require('config/server')
var router = require("config/router")
var constant = require("config/constant")
var navigate = require("util/navigate")
var paging = require('util/paging')
var dialog=sm("do_Dialog")
Controller({
	name : 'index',
	config:{
		name : "userMessage",
		header:{
			title:"我的消息"
		}
	},
	mapping : {
		"lv_mess":"messdata"
	},
	data : {
		paging:paging.create(),
		messdata : [{
			type : "",
			createTime : "",
			content : ""
		}]
	},
	ui:{
		root:'$',
		messlayout:"messlayout",
		lv_mess:"lv_mess"
	},
	event : {
		'lv_mess':{
			'touch':function(index){
				this.readMessage('lv_mess',this.data.messdata[index])
				dialog.open('source://view/dialog/userMess_dialog.ui',this.data.messdata[index].title)
			},
			 /**
	         * 列表下拉刷新
	         */
	        'pull':function(data){
	            if(data.state == 2){
	                this.data.messdata = []
	                this.data.paging.reset()
	                this.getDataMess()
	            }
	        },
	        /**
	         * 列表上拉更新
	         */
	        'push':function(data){
	            if(data.state == 2){
	                if(this.data.paging.next()){
	                	this.getDataMess()
	                }else{
	                    this.ui.lv_mess.rebound()
	                }
	            }
	        }
		},
		
	},
	methods : {
		readMessage:function(idd,data){
			net.send({
				server:server.organization.readUserMessages,
				append: data.id
				,
				success:function(osv){
					this.refresh('lv_mess')
				},
				error : function(data) {
				},
				scope:this
			})
		},
		getDataMess: function () {
			return net.send({
				server:server.organization.getUserMessages,
				data:{sort: "createTime,desc"},
				pag:this.data.paging,
				success:function(mess){
					var list =mess.content.map(function(x) {
						return {
							type:constant.message.type[x.type]||'',
							createTime:x.createTime||'',
							title:x.content||'',
							content:x.title||'',
							id:x.id
						}
					})
					this.data.messdata=list
					if (this.data.messdata.length==0) {
						this.ui.messlayout.bgImage="source://image/app/blank.png"
					}
					this.refresh('lv_mess')
				},
				error : function(data) {
				},
				callback:function(){
                    this.ui.lv_mess.rebound()
                },
				scope:this
			})
		}
	},
	loaded : function() {
		this.updateHeader('我的消息')
		this.loadReady([this.getDataMess()])
	}
})
